package com.healthcloud.controller.modules.patient;

import com.healthcloud.entity.patient.PatientBloodFatEntity;
import com.healthcloud.entity.patient.PatientDataEntity;
import com.healthcloud.service.PatientRecord.PatientBloodFatService;
import com.healthcloud.service.patient.PatientDataService;
import com.healthcloud.utils.ApiRes;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 血脂基本信息
 *
 * @author zhangjie
 */
@Controller
@RequestMapping("bloodFat")
public class BloodFatController {
    @Autowired
    private PatientBloodFatService patientBloodFatService;

    @Autowired
    private PatientDataService patientDataService;

    @ApiOperation(value = "保存血脂基本档案")
    @RequestMapping(value = "/patientBloodFatSave", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes patientBloodFatSave(PatientBloodFatEntity entity) {

        // step1: 血脂基本信息
        PatientBloodFatEntity oldEntity = patientBloodFatService.findByIdCard(entity.getIdCard());
        if (oldEntity == null) {
            // 原有血脂记录不存在，则添加
            patientBloodFatService.save(entity);
        } else {
            // 原有血脂记录已存在，则更新
            entity.setId(oldEntity.getId());
            patientBloodFatService.update(entity);
        }

        /**
         * 病人最后一次数据回显
         */
        // step2: 患者数据
        PatientDataEntity data = new PatientDataEntity();
        data.setIdCard(entity.getIdCard());
        data.setBloodSugar(entity.getBloodSugar());
        data.setHighPressure(entity.getHighPressure());
        data.setLowPressure(entity.getLowPressure());
        data.setHeight(entity.getHeight());
        data.setWeight(entity.getWeight());
        data.setBmi(entity.getBmi());
        data.setWaist(entity.getWaistline());
        data.setHeartRate(entity.getPul());
        data.setTriglyceride(entity.getTriglyceride());
        data.setCholesterol(entity.getCholesterol());
        data.setHighCholesterol(entity.getHighCholesterol());
        data.setLowCholesterol(entity.getLowCholesterol());
        data.setBloodFat(entity.getBloodFat());
        patientDataService.saveOrUpdate(data);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "跳转血脂基本档案修改页面")
    @RequestMapping(value = "/patientBloodFatToSave", method = RequestMethod.GET)
    public String patientBloodFatToSave() {
        return "bus/bloodFat/addOrUpdatePatientBloodFat";
    }

    @ApiOperation(value = "获取血脂基本档案信息")
    @RequestMapping(value = "/patientBloodFat", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes patientBloodFat(String idCard) {
        PatientBloodFatEntity entity = patientBloodFatService.findByIdCard(idCard);
        return new ApiRes(entity);
    }

}
